package cn.chainco.cts.idaoImpl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import cn.chainco.cts.idao.ImDao;
import cn.chainco.cts.jdo.IM;
import cn.chainco.cts.jdo.User;

public class ImDaoImpl extends GenericHibernateDaoImpl<IM, Long> implements ImDao {
	/**
	 * 查询登入人的未读信息
	 */
	@SuppressWarnings("unchecked")
	public List<IM> getOneUserNotReadInfo( User user ){
		String hql = " from IM i where i.receiver=? and i.isRead=0 order by i.sendDate asc";
		return this.getSession().createQuery( hql ).setParameter( 0, user ).list();
	}
	
	//查询删除聊天记录人的id
	private String getTheDelId(User user1, User user2){
		String hql1 = "select i.deleterId from IM i where (i.sender=? or i.receiver=?) and (i.sender=? or i.receiver=?) order by i.sendDate desc limit 0,1";
		
		String deleterId="0";
		List list=this.getSession().createQuery( hql1 ).setParameter( 0, user1 ).setParameter( 1, user1 )
		   .setParameter( 2, user2 ).setParameter( 3, user2 ).list();
		if(list!=null && list.size()!=0 && list.get(0)!=null){
			deleterId=list.get(0).toString();
		}
		return deleterId;
	}
	
	//查询某个人删除记录的日期（清空聊天记录为假删，最新的那天单加入标记，然后以后查该单的发送日期以后的聊天记录）
	private Date getTheNewDate( User user1, User user2){
		String deleterId= getTheDelId(user1,user2);
		String hql1 = "select i.sendDate from IM i where (i.sender=? or i.receiver=?) and (i.sender=? or i.receiver=?) and ? in ("+deleterId+")  order by i.sendDate desc limit 0,1";
		Date date=null;
		List list=this.getSession().createQuery( hql1 ).setParameter( 0, user1 ).setParameter( 1, user1 )
		   .setParameter( 2, user2 ).setParameter( 3, user2 ).setParameter( 4, user1.getId() ).list();
		if(list!=null && list.size()!=0 && list.get(0)!=null){
			date=(Date)list.get(0);
		}
		return date;
	}
	
   /**
    * 查询某个人对某个人的聊天记录
    */	
	@SuppressWarnings("unchecked")
	public List<IM> getChatHistory( User user1, User user2,String content  ){
		Date date=getTheNewDate(user1,user2);
		String hql = " from IM i where (i.sender=? or i.receiver=?) and (i.sender=? or i.receiver=?)";
		if(content!=null && !"".equals(content.trim())){
			hql+=" and i.msgContent like '%"+content+"%'";
		}
		if(date!=null){
			hql +=" and i.sendDate > '"+date+"'";
		}
		
		hql +=" order by i.sendDate asc";
		
		return this.getSession().createQuery( hql ).setParameter( 0, user1 ).setParameter( 1, user1 )
												   .setParameter( 2, user2 ).setParameter( 3, user2 ).list();
	
	}
	
	/**
	 * 查询登入人对某个人的未读信息(user1:收件人，user2:发件人)
	 */
	@SuppressWarnings("unchecked")
	public List<IM> getNotReadChatByOneUser( User user1, User user2){
		String hql = " from IM i where i.receiver=? and i.sender=? and i.isRead=0 order by i.sendDate asc";
		return this.getSession().createQuery( hql ).setParameter( 0, user1 ).setParameter( 1, user2 ).list();
	}
	
	/**
    * 查询某个人对某个人的最新的那条聊天记录
    */	
	@SuppressWarnings("unchecked")
	public IM getNewChat( User user1, User user2){
		String hql = " from IM i where (i.sender=? or i.receiver=?) and (i.sender=? or i.receiver=?) order by i.sendDate desc";
		Query query = this.getSession().createQuery( hql );
		query.setFirstResult(0);
		query.setMaxResults(1);
		List<IM> list = query.setParameter( 0, user1 ).setParameter( 1, user1 )
												   .setParameter( 2, user2 ).setParameter( 3, user2 ).list();
		if( list!=null && list.size()>0 ){
			return list.get(0);
		}else{
			return null;
		}
	}
		
	@SuppressWarnings("unchecked")
	/**
	 * 查询最近联系人的聊天记录
	 */
	public List<IM> getNearIMByUser( User sender){
		String sql = " select * from cts_im where (senderId="+sender.getId()+" or receiverId="+sender.getId()+") ";
		sql +=" group by receiverId,senderId order by sendDate desc limit 0,20";
		return this.getSession().createSQLQuery( sql ).addEntity( IM.class ).list();
	}	
	

	/**
	 * 根据用户查询统计和自己聊过天的人
	 */
	@SuppressWarnings("unchecked")
	public List<IM> getIMHistoryByUser( User sender){
		Date date=getTheNewDate(sender,sender);
		String sql = " select * from cts_im where (senderId="+sender.getId()+" or receiverId="+sender.getId()+") ";
		if(date!=null){
			sql+=" and sendDate>'"+date+"'";
		}
		sql +=" group by senderId,receiverId";
		return this.getSession().createSQLQuery( sql ).addEntity( IM.class ).list();
	}
	
	/**
	 * 统计某个人和多少单聊过天
	 */
	@SuppressWarnings("unchecked")
	public List<IM> getIMHistoryGroupByWork( User sender){
		String sql = " select * from cts_im where (senderId="+sender.getId()+" or receiverId="+sender.getId()+") ";
		sql +=" and workId is not null group by workId";
		return this.getSession().createSQLQuery( sql ).addEntity( IM.class ).list();
	}

}
